package com.yan.utils;

import java.util.concurrent.CountDownLatch;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import com.yan.bean.Link;
import com.yan.comm.Receiver;
import com.yan.mapper.LinkMapper;
import com.yan.service.RedisService;

@SpringBootApplication
@ComponentScan(basePackages = { "com.yan.controller", "com.yan.config", "com.yan.service", "com.yan.requestLimit" })
public class Application2 implements EmbeddedServletContainerCustomizer {

	@Override
	public void customize(ConfigurableEmbeddedServletContainer config) {
		// TODO Auto-generated method stub
		config.setPort(8899);
	}

	public static void main(String[] args) throws InterruptedException {
		ConfigurableApplicationContext appContext = SpringApplication.run(Application2.class, args);

		LinkMapper linkMapper = appContext.getBean("linkMapper", LinkMapper.class);

		Link link = linkMapper.find();

		System.out.println(link);

		RedisService redisService = appContext.getBean("redisService", RedisService.class);
		// String
		redisService.set("redisKey", "yanA");
		String redisValue = (String) redisService.get("redisKey");
		System.out.println(redisValue);

		// String
		redisService.set("redis", "", 1L);
		String redisValues = (String) redisService.get("redis");
		Thread.sleep(1500);
		System.out.println("缓存====" + redisValues);

		// Set
		redisService.add("setAdd", "liangA");
		System.out.println(redisService.setMembers("setAdd"));

		// Hash Map
		redisService.hmSet("hash", "hashKey", "hashValueA");
		System.out.println(redisService.hmGet("hash", "hashKey"));

		redisService.lPush("KOKO", "ASASA");

		RedisTemplate<?, ?> template = (RedisTemplate<?, ?>) appContext.getBean("redisTemplate");
		CountDownLatch latch = appContext.getBean(CountDownLatch.class);

		template.convertAndSend("chat", "Hello from Redis!");
		 
		System.out.println("....."+redisService.get("chat"));
	//	RedisMessageListenerContainer listener= (RedisMessageListenerContainer) appContext.getBean("redisMessageListenerContainer");
		 
		
	 
		latch.await();

	}

}
